<template>
    <div>
        <el-breadcrumb
            separator-class="el-icon-arrow-right"
            class="breadcrumb-base"
            style="line-height: 30px"
        >
            <el-breadcrumb-item>
                <i class="el-icon-printer"></i>
                报告导出
            </el-breadcrumb-item>
        </el-breadcrumb>

        <div class="main-container">
            <el-form :model="filterForm" label-width="70px">
                <el-row>
                    <el-col :span="4">
                        <el-form-item label="检查类型">
                            <el-select
                                v-model="filterForm.checkCategory"
                                placeholder="请选择检查类型"
                                clearable
                                class="picker-form"
                                size="mini"
                            >
                                <el-option
                                    v-for="item in checkCategoryList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="标本类别">
                            <el-select
                                v-model="filterForm.specimenType"
                                placeholder="请选择标本类别"
                                clearable
                                class="picker-form"
                                size="mini"
                            >
                                <el-option
                                    v-for="item in specimenCategoryList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="诊断医生">
                            <el-select
                                v-model="filterForm.doctorId"
                                placeholder="请选择诊断医生"
                                clearable
                                class="picker-form"
                                size="mini"
                            >
                                <el-option
                                    v-for="item in diagnosisDoctorList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item.id"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="诊断时间">
                            <el-date-picker
                                size="mini"
                                style="max-width: 312px"
                                v-model="createdRange"
                                value-format="yyyy-MM-dd HH-mm-ss"
                                type="datetimerange"
                                range-separator="-"
                                start-placeholder="开始时间"
                                end-placeholder="结束时间"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-row>
                            <el-col :offset="1" :span="6">
                                <el-form-item>
                                    <el-button
                                        type="primary"
                                        size="mini"
                                        @click="search"
                                        >查询</el-button
                                    >
                                </el-form-item>
                            </el-col>
                            <!-- <el-col :span="6">
                                <el-form-item>
                                    <el-button
                                        type="primary"
                                        size="mini"
                                    >打印</el-button>
                                </el-form-item>
                            </el-col> -->
                            <el-col :span="6">
                                <el-form-item>
                                    <el-button
                                        type="primary"
                                        size="mini"
                                        @click="exportReport"
                                        >导出</el-button
                                    >
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-col>
                </el-row>
                <el-row style="margin-top: 10px">
                    <base-table
                        ref="biopsyTable"
                        :selected="isSelected"
                        :whichPageHeader="scanMatchedHeader"
                        :data="biopsyTable.data"
                        :header="biopsyTable.header"
                        :operation="biopsyTable.operation"
                        :pageInfo="biopsyTable.pageInfo"
                        :cellStyleFunc="diagnosisTableCellStyleFunc"
                        @handleClick="biopsyTableButtonClick"
                        @pageJumpTo="biopsyTablePageJumpTo"
                        @sizeChange="biopsyTableSizeChange"
                        @selectionChange="selectionChange"
                        @selectAll="handleSelectAll"
                    >
                    </base-table>
                </el-row>
            </el-form>
        </div>
    </div>
</template>
<script>
import { itemsPerPage } from '@/client/libs/util';
import BaseTable from '@templates/components/BaseTable';
export default {
    name: 'DiagnosisReportList',
    components: {
        BaseTable,
    },
    data() {
        return {
            dictRequestMap: [
                {
                    code: 'checkCategory',
                    arrayKey: 'checkCategoryList',
                },
                {
                    code: 'specimenCategory',
                    arrayKey: 'specimenCategoryList',
                },
            ],
            filterForm: {
                diagnosisStartTime: '', //诊断开始时间
                diagnosisEndTime: '', //诊断结束时间
                checkCategory: '', //检查类型
                specimenType: '', //标本类别
                doctorId: '', //诊断医生ID
                page: '', //页数
                size: '', //条数
            },
            createdRange: '', //选的诊断时间范围
            checkCategoryList: [], //检查类型列表
            specimenCategoryList: [], //标本类别列表
            isSelected: true, //是否支持多选
            selectedList: [], //多选的值
            diagnosisDoctorList: [], //诊断医生列表
            scanMatchedHeader: 'DiagnosisReportList', //从哪个页面引用的baseTable组件
            biopsyTable: {
                data: [],
                header: [
                    {
                        label: '病理号',
                        prop: 'pathologyId',
                    },
                    {
                        label: '标本类型',
                        prop: 'specimenName',
                    },
                    {
                        label: '检查类型',
                        prop: 'checkCategoryName',
                    },
                    {
                        label: '诊断',
                        prop: 'subgroupName',
                    },
                    {
                        label: '诊断时间',
                        prop: 'diagnosisTime',
                    },
                    {
                        label: '诊断医生',
                        prop: 'doctorName',
                        width: '107',
                    },
                ],
                operation: [
                    {
                        label: '查看',
                        functionKey: 'watchReport',
                    },
                ],
                // 分页信息
                pageInfo: {
                    size: itemsPerPage.get(),
                    page: 1,
                    total: 0,
                },
            },
        };
    },
    computed: {},
    mounted() {
        this.dictionariesRequest(this.dictionaryMap);
        this.getDoctorList();
        this.biopsyTableRequest();
        if (localStorage.getItem('DiagnosisReportList')) {
            this.biopsyTable.header = JSON.parse(
                localStorage.getItem('DiagnosisReportList')
            );
        }
    },
    methods: {
        search() {
            this.biopsyTableRequest();
        },
        biopsyTableRequest() {
            if (this.createdRange === null) {
                this.createdRange = '';
            }
            let [start, end] = this.createdRange;
            if (this.createdRange) {
                start =
                    start.split(' ')[0] +
                    ' ' +
                    start.split(' ')[1].replace(/-/g, ':');
                end =
                    end.split(' ')[0] +
                    ' ' +
                    end.split(' ')[1].replace(/-/g, ':');
            }
            this.filterForm.diagnosisStartTime = start;
            this.filterForm.diagnosisEndTime = end;
            this.filterForm.page = this.biopsyTable.pageInfo.page - 1;
            this.filterForm.size = this.biopsyTable.pageInfo.size;
            this.$axios({
                method: 'POST',
                url: '/PROXY-Remote/diagnosis/findAll/completeDiagnosisReport',
                data: this.filterForm,
            }).then((res) => {
                let result = res.data;
                result.data.forEach((item) => {
                    item.diagnosisTime =
                        item.diagnosisReportDetailInfosList[0].createTime;
                });
                this.biopsyTable.data = result.data;
                this.biopsyTable.pageInfo = {
                    total: result.pageInfo.total,
                    page: result.pageInfo.page + 1,
                    size: result.pageInfo.size,
                };
            });
        },
        dictionariesRequest() {
            this.dictRequestMap.forEach((map) => {
                this.$axios({
                    method: 'GET',
                    url: '/PROXY-Remote/dicts/list',
                    params: {
                        code: map.code,
                        page: 0,
                        size: 99,
                    },
                }).then((res) => {
                    if (res && res.data.code === 200) {
                        this[map.arrayKey] = _.clone(res.data.data);
                    }
                });
            });
        },
        getDoctorList() {
            this.$axios({
                method: 'POST',
                url: '/PROXY-Remote/users/search',
                data: {
                    roleIdList: [665],
                    statusList: [0],
                    page: 0,
                    size: 999,
                    institutionId: 1,
                },
            }).then((res) => {
                if (res && res.data.code === 200) {
                    this.diagnosisDoctorList = res.data.data;
                }
            });
        },
        diagnosisTableCellStyleFunc({ row, column }) {
            const baseStyle = {
                padding: '3px 0',
                'border-right': '1px solid #ffffff',
                'font-size': '14px',
            };
            return baseStyle;
        },
        // 表格 - 按钮点击
        biopsyTableButtonClick(functionKey, row) {
            this[functionKey](row);
        },
        // 分页跳转
        biopsyTablePageJumpTo(val) {
            this.biopsyTable.pageInfo.page = val;
            this.biopsyTableRequest();
        },
        // 每页显示数量改变
        biopsyTableSizeChange(val) {
            this.biopsyTable.pageInfo.size = val;
            this.biopsyTableRequest();
        },
        selectionChange(selection) {
            this.selectedList = selection;
        },
        handleSelectAll(selection) {
            console.log(selection);
            this.selectedList = selection;
        },
        watchReport(row) {
            const { state } = this.$store;
            const userId = state.user.id;
            const width = window.screen.availWidth + 10;
            const height = window.screen.availHeight + 10;
            window.open(
                `/detail/preview/${userId}/${row.pathologyId}`,
                '_blank',
                `location=no,width=${width},height=${height}`
            );
        },
        exportReport() {
            if (this.selectedList.length > 0) {
                let date = new Date();
                let year = date.getFullYear();
                let month = date.getMonth() + 1;
                let day = date.getDate();
                let hour =
                    date.getHours() < 10
                        ? '0' + date.getHours()
                        : date.getHours();
                let minute =
                    date.getMinutes() < 10
                        ? '0' + date.getMinutes()
                        : date.getMinutes();
                let second =
                    date.getSeconds() < 10
                        ? '0' + date.getSeconds()
                        : date.getSeconds();
                let currentTime =
                    year +
                    '年' +
                    month +
                    '月' +
                    day +
                    '日' +
                    hour +
                    '时' +
                    minute +
                    '分' +
                    second +
                    '秒';
                let txtContent = '';
                let title = '诊断报告列表' + currentTime;
                this.selectedList.forEach((item, index) => {
                    txtContent += `${index + 1}、病理号：${item.pathologyId}\n`;
                    if (item.diagnosticReportDetailInfos.length != 0) {
                        let itemCont = JSON.parse(
                            item.diagnosticReportDetailInfos[0].detailString
                        );
                        if (item.checkCategoryName === '常规病理') {
                            txtContent += `巨检:${
                                itemCont['input_jujian']
                                    ? itemCont['input_jujian']
                                    : '无'
                            },\n病理诊断:${
                                itemCont['input_normal_diagnose']
                                    ? itemCont['input_normal_diagnose']
                                    : '无'
                            },\n`;
                        } else if (item.checkCategoryName === '胃镜病理') {
                            // txtContent += `胃窦:${itemCont['input_weidou']}块,慢性炎症:${itemCont['selection_table_11']},活动性:${itemCont['selection_table_21']},萎缩:${itemCont['selection_table_31']},肠化:${itemCont['selection_table_41']},异性增生:${itemCont['selection_table_51']},HP:${itemCont['selection_table_61']},\n胃角:${itemCont['input_weijiao']}块,慢性炎症:${itemCont['selection_table_12']},活动性:${itemCont['selection_table_22']},萎缩:${itemCont['selection_table_32']},肠化:${itemCont['selection_table_42']},异性增生:${itemCont['selection_table_52']},HP:${itemCont['selection_table_62']},\n胃体:${itemCont['input_weiti']}块,慢性炎症:${itemCont['selection_table_13']},活动性:${itemCont['selection_table_23']},萎缩:${itemCont['selection_table_33']},肠化:${itemCont['selection_table_43']},异性增生:${itemCont['selection_table_53']},HP:${itemCont['selection_table_63']},\n贲门:${itemCont['input_benmen']}块,慢性炎症:${itemCont['selection_table_14']},活动性:${itemCont['selection_table_24']},萎缩:${itemCont['selection_table_34']},肠化:${itemCont['selection_table_44']},异性增生:${itemCont['selection_table_54']},HP:${itemCont['selection_table_64']},\n胃底:${itemCont['input_weidi']}块,慢性炎症:${itemCont['selection_table_15']},活动性:${itemCont['selection_table_25']},萎缩:${itemCont['selection_table_35']},肠化:${itemCont['selection_table_45']},异性增生:${itemCont['selection_table_55']},HP:${itemCont['selection_table_65']},\n其他组织学变化:${itemCont['input_other_organ_change']},\n组织学诊断:${itemCont['input_organ_diagnose']},\n备注:${itemCont['input_remark']},\n`;
                            let weidou = itemCont['input_weidou']
                                ? `胃窦:${itemCont['input_weidou']}块,慢性炎症:${itemCont['selection_table_11']},活动性:${itemCont['selection_table_21']},萎缩:${itemCont['selection_table_31']},肠化:${itemCont['selection_table_41']},异性增生:${itemCont['selection_table_51']},HP:${itemCont['selection_table_61']},\n`
                                : '';

                            let weijiao = itemCont['input_weijiao']
                                ? `胃角:${itemCont['input_weijiao']}块,慢性炎症:${itemCont['selection_table_12']},活动性:${itemCont['selection_table_22']},萎缩:${itemCont['selection_table_32']},肠化:${itemCont['selection_table_42']},异性增生:${itemCont['selection_table_52']},HP:${itemCont['selection_table_62']},\n`
                                : '';
                            let weiti = itemCont['input_weiti']
                                ? `胃体:${itemCont['input_weiti']}块,慢性炎症:${itemCont['selection_table_13']},活动性:${itemCont['selection_table_23']},萎缩:${itemCont['selection_table_33']},肠化:${itemCont['selection_table_43']},异性增生:${itemCont['selection_table_53']},HP:${itemCont['selection_table_63']},\n`
                                : '';
                            let benmen = itemCont['input_benmen']
                                ? `贲门:${itemCont['input_benmen']}块,慢性炎症:${itemCont['selection_table_14']},活动性:${itemCont['selection_table_24']},萎缩:${itemCont['selection_table_34']},肠化:${itemCont['selection_table_44']},异性增生:${itemCont['selection_table_54']},HP:${itemCont['selection_table_64']},\n`
                                : '';
                            let weidi = itemCont['input_weidi']
                                ? `胃底:${itemCont['input_weidi']}块,慢性炎症:${itemCont['selection_table_15']},活动性:${itemCont['selection_table_25']},萎缩:${itemCont['selection_table_35']},肠化:${itemCont['selection_table_45']},异性增生:${itemCont['selection_table_55']},HP:${itemCont['selection_table_65']},\n`
                                : '';
                            let other_organ_change = itemCont[
                                'input_other_organ_change'
                            ]
                                ? `其他组织学变化:${itemCont['input_other_organ_change']},\n`
                                : '';
                            let organ_diagnose = itemCont[
                                'input_organ_diagnose'
                            ]
                                ? `组织学诊断:${itemCont['input_organ_diagnose']},\n`
                                : '';
                            let remark = itemCont['input_remark']
                                ? `备注:${itemCont['input_remark']},\n`
                                : '';
                            txtContent +=
                                weidou +
                                weijiao +
                                weiti +
                                benmen +
                                weidi +
                                other_organ_change +
                                organ_diagnose +
                                remark;

                            // txtContent += `胃窦:${
                            //     itemCont['input_weidou']
                            //         ? itemCont['input_weidou']
                            //         : ' 0 '
                            // }块,慢性炎症:${
                            //     itemCont['selection_table_11']
                            //         ? itemCont['selection_table_11']
                            //         : '    '
                            // },活动性:${
                            //     itemCont['selection_table_21']
                            //         ? itemCont['selection_table_21']
                            //         : '    '
                            // },萎缩:${
                            //     itemCont['selection_table_31']
                            //         ? itemCont['selection_table_31']
                            //         : '    '
                            // },肠化:${
                            //     itemCont['selection_table_41']
                            //         ? itemCont['selection_table_41']
                            //         : '    '
                            // },异性增生:${
                            //     itemCont['selection_table_51']
                            //         ? itemCont['selection_table_51']
                            //         : '    '
                            // },HP:${
                            //     itemCont['selection_table_61']
                            //         ? itemCont['selection_table_61']
                            //         : '    '
                            // },\n胃角:${
                            //     itemCont['input_weijiao']
                            //         ? itemCont['input_weijiao']
                            //         : ' 0 '
                            // }块,慢性炎症:${
                            //     itemCont['selection_table_12']
                            //         ? itemCont['selection_table_12']
                            //         : '    '
                            // },活动性:${
                            //     itemCont['selection_table_22']
                            //         ? itemCont['selection_table_22']
                            //         : '    '
                            // },萎缩:${
                            //     itemCont['selection_table_32']
                            //         ? itemCont['selection_table_32']
                            //         : '    '
                            // },肠化:${
                            //     itemCont['selection_table_42']
                            //         ? itemCont['selection_table_42']
                            //         : '    '
                            // },异性增生:${
                            //     itemCont['selection_table_52']
                            //         ? itemCont['selection_table_52']
                            //         : '    '
                            // },HP:${
                            //     itemCont['selection_table_62']
                            //         ? itemCont['selection_table_62']
                            //         : '    '
                            // },\n胃体:${
                            //     itemCont['input_weiti']
                            //         ? itemCont['input_weiti']
                            //         : ' 0 '
                            // }块,慢性炎症:${
                            //     itemCont['selection_table_13']
                            //         ? itemCont['selection_table_13']
                            //         : '    '
                            // },活动性:${
                            //     itemCont['selection_table_23']
                            //         ? itemCont['selection_table_23']
                            //         : '    '
                            // },萎缩:${
                            //     itemCont['selection_table_33']
                            //         ? itemCont['selection_table_33']
                            //         : '    '
                            // },肠化:${
                            //     itemCont['selection_table_43']
                            //         ? itemCont['selection_table_43']
                            //         : '    '
                            // },异性增生:${
                            //     itemCont['selection_table_53']
                            //         ? itemCont['selection_table_53']
                            //         : '    '
                            // },HP:${
                            //     itemCont['selection_table_63']
                            //         ? itemCont['selection_table_63']
                            //         : '    '
                            // },\n贲门:${
                            //     itemCont['input_benmen']
                            //         ? itemCont['input_benmen']
                            //         : ' 0 '
                            // }块,慢性炎症:${
                            //     itemCont['selection_table_14']
                            //         ? itemCont['selection_table_14']
                            //         : '    '
                            // },活动性:${
                            //     itemCont['selection_table_24']
                            //         ? itemCont['selection_table_24']
                            //         : '    '
                            // },萎缩:${
                            //     itemCont['selection_table_34']
                            //         ? itemCont['selection_table_34']
                            //         : '    '
                            // },肠化:${
                            //     itemCont['selection_table_44']
                            //         ? itemCont['selection_table_44']
                            //         : '    '
                            // },异性增生:${
                            //     itemCont['selection_table_54']
                            //         ? itemCont['selection_table_54']
                            //         : '    '
                            // },HP:${
                            //     itemCont['selection_table_64']
                            //         ? itemCont['selection_table_64']
                            //         : '    '
                            // },\n胃底:${
                            //     itemCont['input_weidi']
                            //         ? itemCont['input_weidi']
                            //         : ' 0 '
                            // }块,慢性炎症:${
                            //     itemCont['selection_table_15']
                            //         ? itemCont['selection_table_15']
                            //         : '    '
                            // },活动性:${
                            //     itemCont['selection_table_25']
                            //         ? itemCont['selection_table_25']
                            //         : '    '
                            // },萎缩:${
                            //     itemCont['selection_table_35']
                            //         ? itemCont['selection_table_35']
                            //         : '    '
                            // },肠化:${
                            //     itemCont['selection_table_45']
                            //         ? itemCont['selection_table_45']
                            //         : '    '
                            // },异性增生:${
                            //     itemCont['selection_table_55']
                            //         ? itemCont['selection_table_55']
                            //         : '    '
                            // },HP:${
                            //     itemCont['selection_table_65']
                            //         ? itemCont['selection_table_65']
                            //         : '    '
                            // },\n其他组织学变化:${
                            //     itemCont['input_other_organ_change']
                            //         ? itemCont['input_other_organ_change']
                            //         : '    '
                            // },\n组织学诊断:${
                            //     itemCont['input_organ_diagnose']
                            //         ? itemCont['input_organ_diagnose']
                            //         : '    '
                            // },\n备注:${
                            //     itemCont['input_remark']
                            //         ? itemCont['input_remark']
                            //         : '    '
                            // },\n`;
                        }
                    }
                });
                this.exportRaw(title, txtContent);
            } else {
                this.$message.info('请选择需要导出的报告数据');
            }
        },
        //导出数据为txt的方法
        exportRaw(name, data) {
            var urlObject = window.URL || window.webkitURL || window;
            var exportBlob = new Blob([data]);
            var saveLink = document.createElement('a');
            saveLink.href = urlObject.createObjectURL(exportBlob);
            saveLink.download = name;
            this.fakeClick(saveLink);
        },
        fakeClick(obj) {
            var ev = document.createEvent('MouseEvents');
            ev.initMouseEvent(
                'click',
                true,
                false,
                window,
                0,
                0,
                0,
                0,
                0,
                false,
                false,
                false,
                false,
                0,
                null
            );
            obj.dispatchEvent(ev);
        },
    },
};
</script>
<style>
</style>
